package com.ruoyi.data.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.SecureUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.SourceType;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.data.domain.bo.getData.UserMd5Bo;
import com.ruoyi.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.data.domain.bo.DataMd5Bo;
import com.ruoyi.data.domain.vo.DataMd5Vo;
import com.ruoyi.data.domain.DataMd5;
import com.ruoyi.data.mapper.DataMd5Mapper;
import com.ruoyi.data.service.IDataMd5Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户md5列Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-06
 */
@RequiredArgsConstructor
@Service
public class DataMd5ServiceImpl implements IDataMd5Service {

    private final DataMd5Mapper baseMapper;
    private final ISysUserService userService;

    /**
     * 查询用户md5列
     */
    @Override
    public DataMd5Vo queryById(Long dataMd5Id){
        return baseMapper.selectVoById(dataMd5Id);
    }

    /**
     * 查询用户md5列列表
     */
    @Override
    public TableDataInfo<DataMd5Vo> queryPageList(DataMd5Bo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DataMd5> lqw = buildQueryWrapper(bo);
        Page<DataMd5Vo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户md5列列表
     */
    @Override
    public List<DataMd5Vo> queryList(DataMd5Bo bo) {
        LambdaQueryWrapper<DataMd5> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DataMd5> buildQueryWrapper(DataMd5Bo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DataMd5> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSmallMd5()), DataMd5::getSmallMd5, bo.getSmallMd5());
        lqw.eq(StringUtils.isNotBlank(bo.getBigMd5()), DataMd5::getBigMd5, bo.getBigMd5());
        lqw.eq(bo.getUserId() != null, DataMd5::getUserId, bo.getUserId());
        return lqw;
    }

    /**
     * 新增用户md5列
     */
    @Override
    public Boolean insertByBo(DataMd5Bo bo) {
        DataMd5 add = BeanUtil.toBean(bo, DataMd5.class);
        validEntityBeforeSave(add);

        add.setSourceType(SourceType.MGR.getSourceType());
        add.setUserId(LoginHelper.getUserId());
        generateData(add);

        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDataMd5Id(add.getDataMd5Id());
        }
        return flag;
    }

    /**
     * 修改用户md5列
     */
    @Override
    public Boolean updateByBo(DataMd5Bo bo) {
        DataMd5 update = BeanUtil.toBean(bo, DataMd5.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DataMd5 entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户md5列
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public DataMd5Vo addUserMd5(UserMd5Bo bo) {

        LoginUser sysUser =  userService.getUserByToken(bo.getToken());

        DataMd5 dataMd5 =  new DataMd5();
        dataMd5.setSourceType(SourceType.WEB.getSourceType());
        dataMd5.setUserId(sysUser.getUserId());
        generateData(dataMd5);
        baseMapper.insert(dataMd5);

        DataMd5Vo update = BeanUtil.toBean(dataMd5, DataMd5Vo.class);
        return  update;
    }


    /**
     * 转换md5
     * @param bo
     */
    private void generateData(DataMd5 bo){

        String md5Data = SecureUtil.md5(bo.getUserData());
        bo.setBigMd5(md5Data.toUpperCase());
        bo.setSmallMd5(md5Data.toLowerCase());

    }
}
